package com.lhy.ltss.controller;

import com.alibaba.fastjson.JSONObject;
import com.lhy.ltss.common.UserRoleConst;
import com.lhy.ltss.entity.Address;
import com.lhy.ltss.service.OrderService;
import com.lhy.ltss.tag.HasLogin;
import com.lhy.ltss.tag.HasRole;
import com.lhy.ltss.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: 李汇远
 */
@RequestMapping("address")
@RestController
public class AddressController {

    @Autowired
    OrderService orderService;

    /**
     * 查询地址信息
     * @return
     */
    @GetMapping
    @HasRole(UserRoleConst.TEACHER)
    public JSONObject find(){
        return ResultJson.ok(orderService.findAddress());
    }

    /**
     * 管理人员根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("id")
    @HasRole(UserRoleConst.ADMIN)
    public JSONObject findById(Integer id){
        return ResultJson.ok(orderService.findAddressById(id));
    }

    /**
     * 添加地址信息
     * @param address
     * @return
     */
    @PostMapping
    @HasRole(UserRoleConst.TEACHER)
    public JSONObject add(Address address){
        return orderService.addAddress(address) > 0 ? ResultJson.ok("添加地址成功") : ResultJson.fail("添加地址失败");
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @HasRole(UserRoleConst.TEACHER)
    public JSONObject remove(Integer id){
        return orderService.removeAddress(id) > 0 ? ResultJson.ok("删除地址成功") : ResultJson.fail("删除失败");
    }

}
